
import {
  USER_LOGIN_SUCCESS,
  LOGIN_DEFEATED,
} from './constants'
import { userAsyncLogin } from '../api/user'

/**
 * 定义登录成功的action
 * */
export const UserSuccessAction = (user) => ({
  type: USER_LOGIN_SUCCESS,
  payload: user,
})

/**
 * 定义登录失败的action
 * */
export const UserResetAction = () =>  ({
  type: LOGIN_DEFEATED,
})

// 异步action登录
export const loginAsyncActionCreator = (user) => (dispatch) => {
  userAsyncLogin(user)
    .then((resData) => {
      console.log(resData)
      dispatch(UserSuccessAction(resData))
    })
    .catch(() => {
      console.log('失败')
    })
}
